package xps.and.uudaijia.userclient.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.ocpsoft.prettytime.PrettyTime;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import xps.and.uudaijia.userclient.R;
import xps.and.uudaijia.userclient.data.baen.CapitalListRes;

public class CapitalAdapterDaijia extends AbstractRecyclerAdapter<CapitalListRes.ReturnBodyBean> {
    CapitalListRes.ReturnBodyBean or;

    PrettyTime p = new PrettyTime(new Locale("ZH_CN"));

    public CapitalAdapterDaijia(Context context, RecyclerView recyclerView) {
        super(context, recyclerView);
    }

    @Override
    protected RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.capital_daijia_item, parent, false);
        v.setOnClickListener(this);
        return new ViewHolder(v);
    }

    @Override
    protected void onBindViewHolder(RecyclerView.ViewHolder holder, int position, boolean isItem) {
        ViewHolder myViewHolder = (ViewHolder) holder;
        or = getItem(position);
        myViewHolder.itemView.setTag(position);
        myViewHolder.tvEndPos.setText("备注:"+or.getRemark());
        myViewHolder.tvStartPos.setText("金额:"+or.getMoney());
        myViewHolder.tvOrderTime.setText(or.getCreateTime());
        orderState(or.getMoneyType(), myViewHolder.tvOrderState);
        parseTime(or.getCreateTime(), myViewHolder.tvOrderTime);
    }

    /**
     * 解析时间
     */
    void parseTime(String createTime, TextView tvOrderTime) {
        Date nowDate = new Date();

        long lTime = Long.valueOf(createTime);
        long nTime = lTime * 1000;
        Date passDate = new Date(nTime);

        if (nowDate.getTime() - passDate.getTime() >= 3600000) {
            //完整格式
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh点mm分");
            tvOrderTime.setText(simpleDateFormat.format(passDate));
        } else {
            //简单格式 sample:  xx分钟之前
            tvOrderTime.setText(p.format(passDate));
        }

    }

    public void orderState(String state, TextView tvOrderState) {
        int orderState = Integer.valueOf(state);
        switch (orderState) {
            case 1:
                tvOrderState.setText("收入");
                break;
            case 2:
                tvOrderState.setText("支出");
                break;
        }
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_order_time)
        TextView tvOrderTime;
        @BindView(R.id.tv_order_state)
        TextView tvOrderState;
        @BindView(R.id.tv_start_pos)
        TextView tvStartPos;
        @BindView(R.id.tv_end_pos)
        TextView tvEndPos;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

    }


}
